<template>
    <div class="access_form_container">
        <el-form ref="accessForm" :rules="rules" :model="formData" label-width="100px" v-enterToNext>
            <el-form-item label="规则编码" prop="code">
                <el-input v-model="formData.code" placeholder="请输入规则编码"></el-input>
            </el-form-item>
            <el-form-item label="规则名称" prop="name">
                <el-input v-model="formData.name" placeholder="请输入规则名称"></el-input>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
export default {
    name: "AccessForm",
    props: {
        propsData: {
            type: Object,
            default() {
                return {
                    init: true,
                };
            },
        },
    },
    data() {
        return {
            rules: {
                code: [ { required: true, message: '请输入规则编码', trigger: 'blur' } ],
                name: [ { required: true, message: '请输入规则名称', trigger: 'blur' } ],
            },
            formData: {},
        };
    },
    watch: {
        propsData: {
            handler() {
                this.init();
            },
            deep: true,
            immediate: true,
        },
    },
    methods: {
        init() {
            const _this = this;
            if (_this.propsData.init) {
                return;
            }
            if (_this.$refs.accessForm) {
                _this.$refs.accessForm.clearValidate();
            }
            _this.formData = $.extend({}, _this.propsData.row);
        },
        async confirm() {
            const _this = this;
            let valid = await _this.validateForm('accessForm');
            if (valid) {
                let subData = $.extend({}, _this.formData);
                return _this.fetchData(_this.$api.API_SaveSysDataObj, subData).then(() => {
                    _this.$message.success('保存成功！');
                    return true;
                }).catch(() => {
                    return false;
                });
            }
            return valid;
        },
    },
}
</script>

<style lang="less" rel="stylesheet/less" scoped>
.access_form_container {
    width: 100%;
    height: 100%;
    overflow: hidden;
}
</style>
